package main

import (
	"fmt"
	"os"
	"runtime"
	"runtime/debug"
	"runtime/trace"
	"time"
)

func GoTrace() {
	f, _ := os.Create("trace.out")
	defer f.Close()
	trace.Start(f)
	defer trace.Stop()
	//循环分配很大内存，触发GC
	for n := 1; n < 100; n++ {
		a := make([]byte, 1<<20)
		a = a
	}

}

func RunTimeDebugReadGc() {
	//每秒触发一次gc
	tracker := time.NewTicker(1 * time.Second)
	gcStat := debug.GCStats{}
	for {
		select {
		case <-tracker.C:
			debug.ReadGCStats(&gcStat)
			fmt.Println(gcStat)
		}
	}
}

func RunTimeDebugReadMem() {
	//每秒触发一次gc
	tracker := time.NewTicker(1 * time.Second)
	gcStat := runtime.MemStats{}
	for {
		select {
		case <-tracker.C:
			runtime.ReadMemStats(&gcStat)
			fmt.Println(gcStat)
		}
	}
}
